<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" id="btn" value="加" onclick="myFoo.doPlus()">
<input type="button" id="btn" value="暂停" onclick="myFoo.doPause()">
<script>
    function $(id){
        return document.getElementById(id);
    }

    function foo(){
        var timer = 0;
        var i = 0;
        function dojia(){
            i++;
            console.log(i);
        }
        var o = {
            doPlus:function(){
                timer = setInterval(dojia,20)
            },
            doPause:function (){
                clearInterval(timer)
            }
        }

        return o;
    }

    var myFoo = foo();





</script>
</body>
</html>